package com.itheima.mall.service.smsService;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.sms.SmsHomeAdvertise;
import com.itheima.mall.commons.PageResult;
import com.itheima.mall.mapper.smsMapper.SmsHomeAdvertiseMapper;
import com.itheima.mall.service.smsService.SmsHomeAdvertiseServiceApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class SmsHomeAdvertiseServiceApiImpl implements SmsHomeAdvertiseServiceApi {
    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;
    /**
     * 添加广告
     * advertise
     */
    @Override
    public void create(SmsHomeAdvertise advertise) {
        smsHomeAdvertiseMapper.insert(advertise);
    }
    /**
     * 删除广告
     * ids
     */
    @Override
    public void delete(List<Long> ids) {
        smsHomeAdvertiseMapper.deleteBatchIds(ids);
    }

    /**
     * 分页查询广告
     * endTime name pageNum pageSize type
     */
    @Override
    public PageResult<SmsHomeAdvertise> list(String endTime, String name, Integer type, Integer pageNum, Integer pageSize) {

        LocalDateTime localEndTime = null;
        if (endTime != null) {
            localEndTime = Convert.toLocalDateTime(endTime);
        }
        IPage<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type != null, SmsHomeAdvertise::getType, type);
        wrapper.eq(localEndTime!=null,SmsHomeAdvertise::getEndTime, localEndTime);
        wrapper.like(name != null, SmsHomeAdvertise::getName, name);
        smsHomeAdvertiseMapper.selectPage(page, wrapper);
        PageResult<SmsHomeAdvertise> pageBean = new PageResult<>(page);
        return pageBean;
    }

    /**
     * 修改上下线状态
     * id status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        smsHomeAdvertiseMapper.updateStatus(id, status);
    }
    /**
     * 修改广告
     * id smsHomeAdvertise
     */
    @Override
    public void updateById(Long id, SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertise.setId(id);
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    /**
     * 获取广告详情
     * id
     */
    @Override
    public SmsHomeAdvertise getAdvertiseById(Long id) {
        return smsHomeAdvertiseMapper.selectById(id);
    }
}
